package com.shou.domain.user.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shou.domain.common.AbstractService;
import com.shou.domain.common.PageQueryEntity;
import com.shou.domain.user.model.entity.UserEntity;
import com.shou.domain.user.repository.IUserRepository;
import com.shou.domain.user.service.IUserService;
import com.shou.types.common.TableNameEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userService")
public class UserService extends AbstractService implements IUserService {

    @Resource
    private IUserRepository userRepository;

    @Override
    public void insertUser(UserEntity userEntity) {
        validateUserEntity(userEntity);
        userRepository.insert(userEntity);
    }

    @Override
    public UserEntity getUserById(Integer id) {
        return userRepository.getUserById(id);
    }

    @Override
    public IPage<UserEntity> pageQuery(PageQueryEntity pageQueryEntity) {
        pageQueryEntity.setSortName(camelToSnake(pageQueryEntity.getSortName()));
        validateQueryParams(pageQueryEntity, TableNameEnum.USER);
        return userRepository.getUsersPage(pageQueryEntity);
    }

    @Override
    public void updateUserById(UserEntity userEntity) {
        Integer id = userEntity.getId();
        if(id == null || !checkIfExistById(id, TableNameEnum.USER)){
            throw new RuntimeException("id为" + id + "的记录不存在");
        }
        validateUserEntity(userEntity, userEntity.getId());
        userRepository.updateUserById(userEntity);
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        super.deleteByIds(ids, TableNameEnum.USER);
    }

    @Override
    public void deleteAll() {
        super.deleteAll(TableNameEnum.USER);
    }

    private void validateUserEntity(UserEntity user, Integer... excludeIds) {
    }
}